<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            var maxProfit = function (prices) {
                let dp = new Array(prices.length).fill().map(() => new Array(2).fill(0))
                dp[0][0] = 0
                dp[0][1] = -prices[0]
                for (let i = 1; i < prices.length; i++) {
                    dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i])
                    dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i])
                }
                return dp[dp.length - 1][0]
            }
            maxProfit([7, 1, 5, 3, 6, 4])
        </script>
    </body>
</html>
